package com.dxl.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxl.base.Result;
import com.dxl.base.common.GlobalConstant;
import com.dxl.base.util.UpDownFileUtil;
import com.dxl.system.mapper.ConfigMapper;
import com.dxl.system.mapper.GroupsMapper;
import com.dxl.system.mapper.NoteMapper;
import com.dxl.system.mapper.SeriesMapper;
import com.dxl.system.model.Config;
import com.dxl.system.model.Groups;
import com.dxl.system.model.Note;
import com.dxl.system.model.Series;
import com.dxl.system.service.NoteService;
import com.dxl.system.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <b> 文件控制器
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Api(value = "文件接口",tags = {"文件--接口"})
@Controller
@RequestMapping("/note")
public class NoteController {

    @Autowired(required = false)
    private NoteService noteService;
    @Autowired(required = false)
    private NoteMapper noteMapper;
    @Autowired(required = false)
    private GroupsMapper groupsMapper;
    @Autowired(required = false)
    private SeriesMapper seriesMapper;
    @Autowired(required = false)
    private ConfigMapper configMapper;
    @Autowired(required = false)
    private TypeService typeService;

    /**
     * <b> 上传文件Html
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "上传文件Html",notes = "")
    @RequestMapping(value = "/upOneHtml",method = {RequestMethod.GET })
    public String upOneHtml() {
        return "note/note_up";
    }

    /**
     * <b> 上传文件
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "上传文件",notes = "")
    @RequestMapping(value = "/upOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result upOne(@RequestParam(value="multipartfile") MultipartFile multipartfile, @RequestParam(value="noteNote") String noteNote, @RequestParam(value="groupId") Integer groupId, @RequestParam(value="seriesId") Integer seriesId, @RequestParam(value="typeSuffix",required = false) String typeSuffix, @RequestParam(value="typeId",required = false) Integer typeId) {
        Result result = noteService.upOne(multipartfile,noteNote,groupId,seriesId,typeSuffix,typeId);
        return result;
    }

    /**
     * <b> 分组树节点上传文件Html
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分组树节点上传文件Html",notes = "")
    @RequestMapping(value = "/upOneTreeHtml",method = {RequestMethod.GET })
    public String upOneTreeHtml(@RequestParam(value="groupId") Integer groupId,Model model) {
        Groups groups = groupsMapper.selectById(groupId);
        model.addAttribute(groups);
        return "note/note_up_tree";
    }

    /**
     * <b> 下载文件
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "下载文件",notes = "")
    @RequestMapping(value = "/downOne",method = {RequestMethod.GET })
    @ResponseBody
    public void downOne(@RequestParam(value="noteId") Integer noteId,HttpServletResponse response) {
        Note note = noteMapper.selectById(noteId);
        Groups groups = groupsMapper.selectById(note.getGroupId());
        QueryWrapper<Config> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.eq("config_key","FileRootPath");
        Config config = configMapper.selectOne(configQueryWrapper);
        if(config.getConfigValue().substring(config.getConfigValue().length()-1).equals("/")){
            config.setConfigValue(config.getConfigValue().substring(0,config.getConfigValue().length()-1));
        }
        UpDownFileUtil.downloadFile(response, config.getConfigValue()+"/"+groups.getNote()+"file/"+note.getNoteTitle()+"."+note.getTypeSuffix(),note.getNoteTitle()+"."+note.getTypeSuffix());
    }

    /**
     * <b> 插入文件
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title  
     * @param note Note
     * @return com.dxl.base.Result Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入文件",notes = "不能为空：文件标题-note_title ；不能重复：文件标题-note_title  ")
    @RequestMapping(value = "/addOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result addOne(@Valid @RequestBody Note note) {
        Result result = noteService.addOne(note);
        return result;
    }

    /**
     * <b> 插入文件Html
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入文件",notes = "不能为空：文件标题-note_title ；不能重复：文件标题-note_title  ")
    @RequestMapping(value = "/addOneHtml",method = {RequestMethod.GET })
    public String addOneHtml() {
        return "note/note_add";
    }

    /**
     * <b> 从分组树上插入文件Html
     * </b><br><br><i>Description</i> : 不能为空：文件标题-note_title ；不能重复：文件标题-note_title
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "从分组树上插入文件Html",notes = "不能为空：文件标题-note_title ；不能重复：文件标题-note_title  ")
    @RequestMapping(value = "/addOneTreeHtml",method = {RequestMethod.GET })
    public String addOneTreeHtml(@RequestParam(value="groupId", required = true) Integer groupId,Model model) {
        Groups groups = groupsMapper.selectById(groupId);
        model.addAttribute(groups);
        return "note/note_add_tree_one";
    }

    /**
     * <b> 分配标签给文件
     * </b><br><br><i>Description</i> :
     * @param noteId Integer，labelIds -List: Integer
     * @return com.dxl.base.Result Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分配标签给文件",notes = "无注意点！")
    @RequestMapping(value = "/addLabelToNote",method = {RequestMethod.POST })
    @ResponseBody
    public Result addLabelToNote(@RequestParam(value="noteId", required = true) Integer noteId, @RequestParam(value="labelIds", required = true) List<Integer> labelIds){
        Result result = noteService.addLabelToNote(noteId, labelIds);
        return result;
    }

    /**
     * <b> 删除文件
     * </b><br><br><i>Description</i> : 
     * @param noteIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除文件",notes = "无注意点！")
    @RequestMapping(value = "/delByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delByIds(@RequestParam(value="noteIds", required = true) List<Integer> noteIds){
        Result result = noteService.delByIds(noteIds);
        return result;
    }
    /**
     * <b> 删除文件
     * </b><br><br><i>Description</i> :
     * @param note
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除文件",notes = "无注意点！")
    @RequestMapping(value = "/delAndFile",method = {RequestMethod.POST })
    @ResponseBody
    public Result delAndFile(@Valid @RequestBody Note note){
        List<Integer> noteIds = new ArrayList<>();
        noteIds.add(note.getId());
        Result result = noteService.delAndFile(noteIds);
        return result;
    }

    /**
     * <b> 移除文件中标签
     * </b><br><br><i>Description</i> :
     * @param noteId Integer，labelIds -List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "移除文件中标签",notes = "无注意点！")
    @RequestMapping(value = "/delLabelFromNote",method = {RequestMethod.POST })
    @ResponseBody
    public Result delLabelFromNote(@RequestParam(value="noteId", required = true) Integer noteId, @RequestParam(value="labelIds", required = true) List<Integer> labelIds) {
Result result = noteService.delLabelFromNote(noteId, labelIds);
        return result;
    }

    /**
     * <b> 移除文件中全部标签
     * </b><br><br><i>Description</i> :
     * @param noteId Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "移除文件中全部标签",notes = "无注意点！")
    @RequestMapping(value = "/delAllLabelFromNote",method = {RequestMethod.POST })
    @ResponseBody
    public Result delAllLabelFromNote(@RequestParam(value="noteId", required = true) Integer noteId) {
Result result = noteService.delAllLabelFromNote(noteId);
        return result;
    }

    /**
     * <b> 编辑文件
     * </b><br><br><i>Description</i> : 不能重复：文件标题-note_title 
     * @param note Note
     * @return com.dxl.base.Result Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑文件",notes = "不能重复：文件标题-note_title ")
    @RequestMapping(value = "/editOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result editOne(@Valid @RequestBody Note note) {
        Result result = noteService.editOne(note);
        return result;
    }

    /**
     * <b> 编辑文件Html
     * </b><br><br><i>Description</i> : 不能重复：文件标题-note_title
     * @param noteId
     * @return com.dxl.base.Result Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑文件Html",notes = "不能重复：文件标题-note_title ")
    @RequestMapping(value = "/editOneHtml",method = {RequestMethod.GET })
    public String editOneHtml(@RequestParam(value="noteId", required = true) Integer noteId, Model model) {
        Note note = noteMapper.selectById(noteId);
        Groups groups = groupsMapper.selectById(note.getGroupId());
        Series series = new Series();
        if(note.getSeriesId() != null){
            series = seriesMapper.selectById(note.getSeriesId());
        }
        model.addAttribute(note);
        model.addAttribute(groups);
        model.addAttribute(series);
        return "note/note_edit";
    }

    /**
     * <b> 选择文件Html
     * </b><br><br><i>Description</i> : 不能重复：文件标题-note_title
     * @return com.dxl.base.Result Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑文件Html",notes = "不能重复：文件标题-note_title ")
    @RequestMapping(value = "/selectLableHtml",method = {RequestMethod.GET })
    public String selectLableHtml(@RequestParam(value="noteId", required = true) Integer noteId,Model model) {
        Note note = noteMapper.selectById(noteId);
        model.addAttribute(note);
        return "note/note_select_label";
    }
    /**
     * 获取html的CSS列表
     */
    @GetMapping(value = "/selectHtmlCssHtml")
    public String selectHtmlCssHtml(@RequestParam(value="noteId", required = true) Integer noteId,Model model) {
        List<String> htmlCssList = Arrays.asList(GlobalConstant.HtmlCssList.replace("，",",").split(","));
        Note note = (Note) noteService.getById(noteId).getData();
        note.setHtmlCssList(htmlCssList);
        model.addAttribute(note);
        return "note/note_select_html_css";
    }

    /**
     * <b> ID查询文件
     * </b><br><br><i>Description</i> : 
     * @param noteId Integer
     * @return com.dxl.base.Result: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "ID查询文件",notes = "无注意点！")
    @RequestMapping(value = "/getById",method = {RequestMethod.POST })
    @ResponseBody
    public Result getById(@RequestParam(value="noteId", required = true) Integer noteId) {
        Result result = noteService.getById(noteId);
        return result;
    }

    /**
     * <b> 条件查询文件
     * </b><br><br><i>Description</i> : 
     * @param note Note
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "条件查询文件",notes = "无注意点！")
    @RequestMapping(value = "/getList",method = {RequestMethod.POST })
    @ResponseBody
    public Result getList(@Valid @RequestBody Note note) {
        Result result = noteService.getList(note);
        return result;
    }

    /**
     * <b> 分页条件查询文件
     * </b><br><br><i>Description</i> : 
     * @param note Note, currentPage -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询文件",notes = "无注意点！")
    @RequestMapping(value = "/getListByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListByPage(@Valid @RequestBody Note note, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = noteService.getListByPage(note, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 条件查询分组及子分组所有文件
     * </b><br><br><i>Description</i> :
     * @param note Note
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "条件查询文件",notes = "无注意点！")
    @RequestMapping(value = "/getListAllInGroup",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListAllInGroup(@Valid @RequestBody Note note) {
        Result result = noteService.getListAllInGroup(note,null);
        return result;
    }

    /**
     * <b> 分页条件查询分组及子分组所有文件
     * </b><br><br><i>Description</i> :
     * @param note Note, currentPage -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询文件",notes = "无注意点！")
    @RequestMapping(value = "/getListAllInGroupByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListAllInGroupByPage(@Valid @RequestBody Note note, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = noteService.getListAllInGroupByPage(note, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 查询文件中全部标签
     * </b><br><br><i>Description</i> : 联表
     * @param noteId Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "查询文件中全部标签",notes = "无注意点！")
    @RequestMapping(value = "/getAllLabelInNote",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllLabelInNote(@RequestParam(value="noteId", required = true) Integer noteId) {
        Result result = noteService.getAllLabelInNote(noteId);

        return result;
    }

    /**
     * <b> 分页查询文件中全部标签
     * </b><br><br><i>Description</i> : 联表
     * @param noteId Integer, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分页查询文件中全部标签",notes = "无注意点！")
    @RequestMapping(value = "/getAllLabelInNoteByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllLabelInNoteByPage(@RequestParam(value="noteId", required = true) Integer noteId, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = noteService.getAllLabelInNoteByPage(noteId, currentPage, pageSize);
        return result;
    }

    /**
     * <b>  模糊查询文件
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：note_title-文件标题 note-备注 
     * @param note Note
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "模糊查询文件",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：note_title-文件标题 note-备注 ")
    @RequestMapping(value = "/getLikeNote",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeNote(@Valid @RequestBody Note note){
        Result result = noteService.getLikeNote(note);
        return result;
    }

    /**
     * <b> 分页模糊查询文件
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：note_title-文件标题 note-备注 
     * @param note Note, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页模糊查询文件",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：note_title-文件标题 note-备注 ")
    @RequestMapping(value = "/getLikeNoteByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeNoteByPage(@Valid @RequestBody Note note, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize){
        Result result = noteService.getLikeNoteByPage(note, currentPage, pageSize);
        return result;
    }

    /**
     * <b>  联表查询文件
     * </b><br><br><i>Description</i> : 联表查询，联表：标签文件关联 标签
     * @param note Note
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-29    <br>Author : dxl
     */
    @ApiOperation(value = "联表查询文件",notes = "联表查询，联表：标签文件关联 标签 ")
    @RequestMapping(value = "/getJoinLabelSeries",method = {RequestMethod.POST })
    @ResponseBody
    public Result getJoinLabelSeries(@Valid @RequestBody Note note){
        Result result = noteService.getJoinLabelSeries(note);
        return result;
    }

    /**
     * <b> 分页联表查询文件
     * </b><br><br><i>Description</i> : 联表查询，联表：标签文件关联 标签
     * @param note Note, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-29     <br>Author : dxl
     */
    @ApiOperation(value = "分页联表查询文件",notes = "联表查询，联表：标签文件关联 标签 ")
    @RequestMapping(value = "/getJoinLabelSeriesByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getJoinLabelSeriesByPage(@Valid @RequestBody Note note, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize){
        Result result = noteService.getJoinLabelSeriesByPage(note, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 文件能否新增
     * </b><br><br><i>Description</i> : 无权限限制；不能为空：文件标题-note_title ；不能重复：文件标题-note_title  
     * @param note Note
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "文件能否新增",notes = "无权限限制；不能为空：文件标题-note_title ；不能重复：文件标题-note_title  ")
    @RequestMapping(value = "/isAddable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isAddable(@Valid @RequestBody Note note) {
        Result result = noteService.isNoteAddable(note);
        return result;
    }

    /**
     * <b> 文件能否编辑
     * </b><br><br><i>Description</i> : 无权限限制；不能重复：文件标题-note_title 
     * @param note Note
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "文件能否编辑",notes = "无权限限制；不能重复：文件标题-note_title ")
    @RequestMapping(value = "/isEditable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isEditable(@Valid @RequestBody Note note) {
        Result result = noteService.isNoteEditable(note);
        return result;
    }
}
